package com.smile.controller;

import com.smile.api.FlyApi;
import com.smile.commons.gps.CoordinateTransformUtil;
import com.smile.commons.page.R;
import com.smile.model.dto.request.FlyRequestDTO;
import com.smile.model.dto.request.FlyStickControlRequestDTO;
import com.smile.model.request.*;
import com.smile.model.response.response.GetFlyResultResponseDTO;
import com.smile.service.AirportService;
import com.smile.service.FlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/fly")
public class FlyController {

    @Autowired
    private AirportService airportService;

    @Autowired
    FlyApi flyApi;

    @Autowired
    FlyService flyService;
    /**
     * 一键起飞
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/takeoffToPoint")
    public R<Map<String, Object>> takeoffToPoint(@RequestBody FlyRequestDTO request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        GetFlyResultResponseDTO flyResultResponseDTO = flyService.takeoffToPoint(request);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            //起飞成功后执行指令控制
            GetFlyResultResponseDTO flyResultResponseDTO2 = flyService.drcModeEnter(request);
            map.put("message", "指令执行成功");
            map.put("errorCode",0 );
            map.put("flighttaskStepCode",5);
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            map.put("message", flyResultResponseDTO.getMessage());
            map.put("errorCode",flyResultResponseDTO.getCode());
            map.put("flighttaskStepCode",flyResultResponseDTO.getFlighttaskStepCode());
        }
        return R.ok(map);
    }

    /**
     * 返航
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/returnHome")
    public R<String> returnHome(@RequestBody FlyRequestDTO request) throws Exception {
//        return R.ok("success");
        GetFlyResultResponseDTO flyResultResponseDTO = flyService.returnHome(request);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            return R.ok("success");
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            return R.fail(flyResultResponseDTO.getMessage());
        }
    }

    /**
     * 飞向目标点
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/flyToPoint")
    public R<Void> flyToPoint(@RequestBody FlyToPointRequest flyToPointRequest) throws Exception {
        System.out.println("takeoffToPoint");
//        "maxSpeed":20.00,
//                "height":400.00,
        CoordinateTransformUtil.Coordinate coordinate =  CoordinateTransformUtil.gcj02ToWgs84(flyToPointRequest.getLongitude(), flyToPointRequest.getLatitude());
        flyToPointRequest.setLongitude(coordinate.getLongitude());
        flyToPointRequest.setLatitude(coordinate.getLatitude());
        flyToPointRequest.setHeight(BigDecimal.valueOf(120));
        flyToPointRequest.setMaxSpeed(BigDecimal.valueOf(12));
        flyApi.flyToPoint(flyToPointRequest);
        return R.ok();
    }

    /**
     * 直播
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/liveStartPush")
    public R<String> liveStartPush(@RequestBody LiveStartPushRequest flyToPointRequest) throws Exception {
        System.out.println("takeoffToPoint");
        flyApi.liveStartPush(flyToPointRequest);
        return R.ok();
    }


    /**
     * 抢夺
     * @return
     * @throws Exception
     */
    @PostMapping("/flightAuthorityGrab")
    public R<String> flightAuthorityGrab(@RequestBody FlyRequestDTO flyRequestDTO) throws Exception {
        GetFlyResultResponseDTO flyResultResponseDTO = flyService.flightAuthorityGrab(flyRequestDTO);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            return R.ok("success");
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            return R.fail(flyResultResponseDTO.getMessage());
        }
    }


    /**
     * 进入指令飞行模式
     * @return
     * @throws Exception
     */
    @PostMapping("/drcModeEnter")
    public R<String> drcModeEnter(@RequestBody FlyRequestDTO flyRequestDTO) throws Exception {
        GetFlyResultResponseDTO flyResultResponseDTO = flyService.drcModeEnter(flyRequestDTO);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            return R.ok("success");
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            return R.fail(flyResultResponseDTO.getMessage());
        }
    }

    /**
     * 杆量控制 暂时无法执行成功
     * @return
     * @throws Exception
     */
    @PostMapping("/stickControl")
    public R<String> stickControl(@RequestBody FlyStickControlRequestDTO  flyStickControlRequestDTO) throws Exception {
        GetFlyResultResponseDTO flyResultResponseDTO = flyService.stickControl(flyStickControlRequestDTO);
        if (flyResultResponseDTO.getStatus() == Boolean.TRUE) {
            return R.ok("success");
        }else{
            System.out.println(flyResultResponseDTO.getMessage());
            return R.fail(flyResultResponseDTO.getMessage());
        }
    }


    /**
     * 飞行控制
     * @return
     * @throws Exception
     */
    @PostMapping("/droneControl")
    public R<String> droneControl(@RequestBody DroneControlRequest  droneControlRequest) throws Exception {
        flyApi.droneControl(droneControlRequest);
        return R.ok("success");
    }

    /**
     * 飞行器急停
     * @return
     * @throws Exception
     */
    @PostMapping("/droneEmergencyStop")
    public R<String> droneEmergencyStop(@RequestBody DroneEmergencyStopRequest  droneEmergencyStopRequest) throws Exception {
        flyApi.droneEmergencyStop(droneEmergencyStopRequest);
        return R.ok("success");
    }
}
